package com.zy.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.business.domain.TbDocument;
import com.zy.business.domain.TbDocumentFolder;
import com.zy.business.mapper.DocumentMapper;
import com.zy.business.service.DocumentService;
import com.zy.common.core.domain.AjaxResult;
import com.zy.common.exception.ServiceException;
import com.zy.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description: 文档实现类
 * @author: whf
 * @create: 2023-02-24 15:03
 **/
@Service
public class DocumentServiceImpl extends ServiceImpl<DocumentMapper, TbDocument> implements DocumentService {
    @Override
    public int deleteDocument(Long documentId) {
        TbDocument document = baseMapper.selectById(documentId);
        if (SecurityUtils.getUsername().equals(document.getCreateBy()) || SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            int row = baseMapper.deleteById(documentId);
            return row;
        }
        throw new ServiceException("您没有权限删除该文件");
    }

    @Override
    public AjaxResult addDocument(TbDocumentFolder documentFolder) {
        List<TbDocument> documentList = documentFolder.getDocumentList();
        if (documentList!=null && documentList.size()>0) {
            documentList.forEach(document -> {
                document.setDocumentFolderId(documentFolder.getId());
                document.setCreateBy(SecurityUtils.getUsername());
                document.setCreateTime(new Date());
                baseMapper.insert(document);
            });
        }
        return AjaxResult.success();
    }
}
